package com.nf.health.app.sport_recipe.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.hostopal.MyHostipalListActivity;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.SportRecipePopupWindow;
import com.nf.health.app.sport_recipe.fragment.SportAttentionFragment;
import com.nf.health.app.sport_recipe.fragment.SportRecipeFragment;
import com.nf.health.app.sport_recipe.fragment.SportUserInfoFragment;
import com.nf.health.app.sweat.activity.SweatAnalyzerHistoryTrackActivity;
import com.nf.health.app.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by changpeng on 2016/11/10.
 * 和运动处方一样，只不过如果是从历史运动记录跳过来就是这个页面
 */

public class SportRecipeHisActivity extends BaseActivity {

    @Bind(R.id.tabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.vp_content)
    ViewPager mViewPager;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.titlebar_right_view)
    ImageView titlebarRightView;
    Handler handler = new Handler();
    RelativeLayout rlTitleBack;
    TextView tvGoHospital;
    public static String hpuid = "";
    private SportUserInfoFragment mUserInfoFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.layout_common_header);
        setContent(R.layout.activity_sport_recipe);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initView() {
        hpuid = getIntent().getStringExtra(Constants.HPUID);
        titlebarTitleTv.setText("运动处方");
        titlebarRightView.setImageResource(R.drawable.intervention_menu);
        MyViewPagerAdapter viewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        mUserInfoFragment = new SportUserInfoFragment();
        viewPagerAdapter.addFragment(mUserInfoFragment, "个人信息");
        viewPagerAdapter.addFragment(new SportRecipeFragment(), "运动处方");
        viewPagerAdapter.addFragment(new SportAttentionFragment(), "注意事项");
        mViewPager.setAdapter(viewPagerAdapter);//设置适配器
        mTabLayout.setupWithViewPager(mViewPager);

    }

    private void initListener() {
        titlebarRightView.setOnClickListener(this);
    }



    public interface IOpFragment{
        void onAction(String hpuid);
    }



    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
//        if (Act.Flag.FLAG_GET_LATEST_SPORTRECIPE_ID.equals(actionName)) {
//            hpuid = (String) object;
//            System.out.println("请求结果" + hpuid);
//            if ("0".equals(hpuid)) {
////                setContent(R.layout.sport_recipe_no);
//                setContentView(R.layout.sport_recipe_no); //用此方法才能是系统的状态栏透明
//                initNoDataViewAndListener();
//            } else {
//
//
//                if(getIntent().getStringExtra("hpuid") != null){
//                    hpuid = getIntent().getStringExtra("hpuid");
//                }
//                mUserInfoFragment.onAction(hpuid);
//            }
//        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.titlebar_right_view:
                showPop();
                break;
            case R.id.rl_title_back:
                finish();
                break;
            case R.id.tv_go_hospital:
                ActivityUtils.showActivity(this, MyHostipalListActivity.class);
                break;
            default:
                break;
        }
    }


    /**
     * 右上角popupwindow
     */
    private void showPop() {
        //使除去popupwindow部分为灰色
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.5f;
        getWindow().setAttributes(lp);
        SportRecipePopupWindow pop = new SportRecipePopupWindow(this);
        //设置消失监听
        pop.setOnDismissListener(new OnPopDismissListener());
        //设置menu点击监听
        pop.setOnMyItemClickListener(new onPopMenuClickListenr());
        pop.showAsDropDown(titlebarRightView);
    }

    /**
     * pop消失回调
     */
    private class OnPopDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            final WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 1.0f;
            getWindow().setAttributes(lp);
        }
    }

    /**
     * pop条目点击监听
     */
    private class onPopMenuClickListenr implements OnMyClickListener {
        @Override
        public void onClick(int position, String flag) {
            if ("0".equals(flag)) {
                switch (position) {
                    case 0:
                        //跑步机
                        Intent intent = new Intent(SportRecipeHisActivity.this, EnduranceTestActivity.class);
                        intent.putExtra("hpuid",hpuid);
                        startActivity(intent);
                        break;
                    case 1:
                        //心藏
                        ActivityUtils.showActivity(SportRecipeHisActivity.this, HouseMindActivity.class);
                        break;
                    case 2:
                        //汗液分析
                        ActivityUtils.showActivity(SportRecipeHisActivity.this, SweatAnalyzerHistoryTrackActivity.class);
                        break;
                }
            } else if ("1".equals(flag)) {
                //1代表pop中其他view的点击,position是view的id
                switch (position) {
                    case R.id.tv_history://历史运动处方
                        ActivityUtils.showActivity(SportRecipeHisActivity.this, SportHistoryActivity.class);
                        break;
                    default:
                        break;
                }
            }
        }
    }


    public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();//添加的Fragment的集合
        private final List<String> mFragmentsTitles = new ArrayList<>();//每个Fragment对应的title的集合

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * @param fragment      添加Fragment
         * @param fragmentTitle Fragment的标题，即TabLayout中对应Tab的标题
         */
        public void addFragment(Fragment fragment, String fragmentTitle) {
            mFragments.add(fragment);
            mFragmentsTitles.add(fragmentTitle);
        }

        @Override
        public Fragment getItem(int position) {
            //得到对应position的Fragment
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            //返回Fragment的数量
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            //得到对应position的Fragment的title
            return mFragmentsTitles.get(position);
        }
    }
}
